export default {
  name: 'co-confirm',
  props: {
    title: String,
    content: String,
    className: String,
    okLabel: String,
    cancelLabel: String,
    beforeClose: Function,
    showCancel: Boolean
  },
  data () {
    return {
      open: false
    };
  },
  methods: {
    handleClose (result) {
      if (this.beforeClose) {
        return this.beforeClose(result, this, () => this.close(result));
      }
      return this.close(result);
    },
    close (isOk) {
      this.open = false;
      this.$emit('close', isOk);
    },
    createActions (h) {
      const actions = [];
      actions.push(
        h('co-button', {
          props: {
            color: 'primary'
          },
          slot: 'action',
          on: {
            click: () => this.handleClose(true)
          }
        }, this.okLabel)
      );
      if (this.showCancel) {
        actions.unshift(h('co-button', {
          props: {
            flat: true
          },
          slot: 'action',
          on: {
            click: () => this.handleClose(false)
          }
        }, this.cancelLabel));
      }
      return actions;
    }
  },
  render (h) {
    return h('co-dialog', {
      props: {
        open: this.open,
        title: this.title,
        dialogClass: 'confirm-dialog ' + (this.className || ''),
        overlayClose: false,
        escPressClose: false
      }
    }, [
      this.content,
      ...this.createActions(h)
    ]);
  }
};
